
package com.winning.sx.microframework.controller.base;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import com.winning.sx.microframework.service.sysmanage.RoleManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cust/organization")
public class CustOrganizationController {
    @Autowired
    EasyService easyService;
    @Autowired
    RoleManageService service;
    @Autowired
    ConstService services;
   @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
       ModelAndView mv = new ModelAndView("/base/customer");
       DataTable all = service.queryRoles();
       List staffRole = all.getDataRows();
       mv.addObject("staffRole", staffRole);
       List state= services.queryCount("200");
       mv.addObject("state",state);
       List validState = services.queryCount("100");
       mv.addObject("validState", validState);
       List organizationType= services.queryCount("400");
       mv.addObject("organizationType",organizationType);
       List warehouseFlag= services.queryCount("401");
       mv.addObject("warehouseFlag",warehouseFlag);
       List staffType= services.queryCount("311");
       mv.addObject("staffType",staffType);
       Map organization=new HashMap();
       organization.put("superOrganizationId","0");
       organization.put("organizationType","400001");
       DataTable  organizationEntity = easyService.queryList("BASE_Organization",organization);
       List organizationName = organizationEntity.getDataRows();
       mv.addObject("organizationName", organizationName);
        return mv;
    }
    // 客户机构管理查询
    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryMessageList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            Map params=new HashMap();
            if(param.get("oneOrganization")==null||"0".equals(param.get("oneOrganization").toString())){
              params.put("superOrganizationId",param.get("oneOrganization").toString());
              params.put("organizationType","400001");
            }else{
                String oneOrganization=param.get("oneOrganization").toString().replaceAll(",","");
                params.put("superOrganizationId",oneOrganization);
            }
            params.put("validFlag","100001");
            DataTable all = easyService.querySql("BASE_Organization", "*",params,"","createTime desc","");
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/save"})
    public AjaxDataResponseMessage insert(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                if(!"".equals(map.get("superOrganizationId").toString())){
                    String superOrganizationId= map.get("superOrganizationId").toString().replaceAll(",","");
                    map.put("superOrganizationId",superOrganizationId);
                    Map param=new HashMap();
                    param.put("id",superOrganizationId);
                    Map entity=easyService.queryEntity("BASE_Organization",param);
                    if(entity!=null){
                        map.remove("organizationType");
                        map.put("organizationType",entity.get("organizationType").toString());
                    }
                }
                map.put("state","200001");
                    easyService.save("BASE_Organization", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                    easyService.delete("BASE_Organization", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("删除失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
}

